package com.cxx.purchasecharge.dal.impl;

import java.util.List;

import javax.persistence.Query;

import org.apache.commons.collections.CollectionUtils;

import com.cxx.purchasecharge.core.model.persistence.GoodsType;
import com.cxx.purchasecharge.dal.GoodsTypeDao;
import com.cxx.purchasecharge.dal.common.MyGenericDaoImpl;

public class GoodsTypeDaoImpl extends MyGenericDaoImpl<GoodsType, Long> implements GoodsTypeDao {

	public GoodsTypeDaoImpl() {
		super(GoodsType.class);
	}

	@Override
	public GoodsType findByName(String name) {
		String sql = "select g from GoodsType g where g.name=?1";
		Query query = getEntityManager ().createQuery (sql, GoodsType.class);
		query.setParameter (1, name);
		return CollectionUtils.isNotEmpty (query.getResultList ()) ? (GoodsType)query.getResultList ().get (0) : null;
	}

	@Override
	public List<GoodsType> findByParent(Long parentId) {
		String sql = "select g from GoodsType g where g.parent.id=?1";
		Query query = getEntityManager ().createQuery (sql, GoodsType.class);
        query.setParameter (1, parentId);
		return query.getResultList ();
	}

}
